package com.jhhc.WaterQuality.Calculate.Forecast.util;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@Service
public class CalculateUtil {

    // 读取输入流和错误流的线程
    static class StreamReader implements Runnable {
        private InputStream inputStream;

        StreamReader(InputStream inputStream) {
            this.inputStream = inputStream;
        }

        @Override
        public void run() {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                    // 处理命令输出
                    System.out.println(line);
                }
                reader.close();
            } catch (IOException e) {
                System.out.println("读取命令输出时出现异常：" + e.getMessage());
            }
        }
    }
    public String res() {
        try {
            // 指定要执行的命令
            String command = "your_command_here";

            // 创建ProcessBuilder对象，并指定要执行的命令
            ProcessBuilder processBuilder = new ProcessBuilder(command);

            // 启动进程并执行命令
            Process process = processBuilder.start();

            // 获取进程的输入流和错误流
            InputStream inputStream = process.getInputStream();
            InputStream errorStream = process.getErrorStream();

            // 创建读取输入流和错误流的线程
            Thread inputStreamThread = new Thread(new StreamReader(inputStream));
            Thread errorStreamThread = new Thread(new StreamReader(errorStream));

            // 启动线程
            inputStreamThread.start();
            errorStreamThread.start();

            // 等待进程执行完成
            int exitCode = process.waitFor();

            // 等待输入流和错误流线程执行完成
            inputStreamThread.join();
            errorStreamThread.join();

            System.out.println("命令执行完成！");
        } catch (IOException | InterruptedException e) {
            System.out.println("执行命令时出现异常：" + e.getMessage());
        }
return  null;
    }






        public  String convertString(String str) {
            StringBuilder stringBuilder = new StringBuilder();

            // 去除字符串中的小数点
            String strWithoutDecimal = str.replace(".", "");

            // 判断是否为负数
            boolean isNegative = strWithoutDecimal.startsWith("-");

            // 去除负号
            if (isNegative) {
                strWithoutDecimal = strWithoutDecimal.substring(1);
            }

            // 补足长度为5
            while (strWithoutDecimal.length() < 5) {
                strWithoutDecimal = "0" + strWithoutDecimal;
            }

            // 添加负号（如果是负数）
            if (isNegative) {
                strWithoutDecimal = "-" + strWithoutDecimal;
            }

            return strWithoutDecimal;
        }
        public  String reverseConvert(String str) {
            StringBuilder stringBuilder = new StringBuilder();

            // 判断是否为负数
            boolean isNegative = str.startsWith("-");

            // 去除负号
            if (isNegative) {
                str = str.substring(1);
            }

            // 截取整数部分
            String intPart = str.substring(0, str.length() - 2);

            // 截取小数部分
            String decimalPart = str.substring(str.length() - 2);

            // 添加小数点和小数部分
            stringBuilder.append(intPart).append(".").append(decimalPart);

            // 添加负号(如果是负数)
            if (isNegative) {
                stringBuilder.insert(0, "-");
            }

            return stringBuilder.toString();
        }
    }


